import * as vscode from 'vscode';
import { VSCodeASTParser } from './vscodeAstParser';
import { RegexActionParser } from './regexActionParser';

/**
 * Hybrid action parser that uses VS Code's built-in TypeScript services when available,
 * falling back to regex-based parsing for maximum compatibility and minimal bundle size
 */
export class HybridActionParser {
  
  /**
   * Extract action names from module files with fallback strategy
   */
  static async getModuleActionNames(
    filePath: string, 
    modelName: string='', 
    innerActions: string[]=[], 
    context?: vscode.ExtensionContext
  ): Promise<string[]> {
    try {
      // First, try using VS Code's built-in TypeScript service
      const vscodeResults = await VSCodeASTParser.getModuleActionNamesWithVSCode(filePath, modelName, innerActions, context);
      
      // If VS Code service returns results, use them
      if (vscodeResults.length > 0) {
        console.log('Using VS Code TypeScript service results for', filePath);
        return vscodeResults;
      }
      
      // Fallback to regex parsing if VS Code service fails or returns no results
      console.log('Falling back to regex parsing for', filePath);
      return RegexActionParser.getModuleActionNames(filePath, modelName, innerActions);
      
    } catch (error) {
      console.error('Error in hybrid parsing, using regex fallback:', error);
      return RegexActionParser.getModuleActionNames(filePath, modelName, innerActions);
    }
  }
  
  /**
   * Extract root store action names with fallback strategy
   */
  static async getRootActionNames(
    filePath: string, 
    innerActions?: string[], 
    context?: vscode.ExtensionContext
  ): Promise<string[]> {
    try {
      // First, try using VS Code's built-in TypeScript service
      const vscodeResults = await VSCodeASTParser.getRootActionNamesWithVSCode(filePath, context);
      
      // If VS Code service returns results, use them
      if (vscodeResults.length > 0) {

        console.log('Using VS Code TypeScript service results for root store', filePath);
        return [...(innerActions ? innerActions : []), ...vscodeResults];
      }
      
      // Fallback to regex parsing
      console.log('Falling back to regex parsing for root store', filePath);
      return RegexActionParser.getRootActionNames(filePath, innerActions);
      
    } catch (error) {
      console.error('Error in hybrid root parsing, using regex fallback:', error);
      return RegexActionParser.getRootActionNames(filePath, innerActions);
    }
  }
  
  /**
   * Synchronous version for backwards compatibility (uses regex only)
   */
  static getModuleActionNamesSync(filePath: string, modelName?: string, innerActions?: string[]): string[] {
    return RegexActionParser.getModuleActionNames(filePath, modelName, innerActions);
  }
  
  /**
   * Synchronous version for backwards compatibility (uses regex only)
   */
  static getRootActionNamesSync(filePath: string, innerActions?: string[]): string[] {
    return RegexActionParser.getRootActionNames(filePath, innerActions);
  }
}